$(document).ready(function() {
	var arr = area();
	for(var i=0; i<arr.length;i++){
		$("#change").append("<option value="+arr[i].areaCode+">"+arr[i].province+" "+arr[i].city+" "+arr[i].area+"</option>");
	}
	init(1);
	changearea();
});
	
//默认加载  
function init(pageNo){
	var areaCode = $("#change").val();
	//获取信息列表
	$("#thislist").html("");
	$.ajax({
		url: urlcore + "/api/sendRates/jpa/page?pageNum="+pageNo+"&pageSize="+pageSize+"&areaCode="+areaCode,
		type: "get",
		dataType: 'json',
		contentType: "application/json;charset=utf-8",
		success:function(data){
			if (data.success == true) {
				//i表示在data中的索引位置，n表示包含的信息的对象
				$.each(data.data.list,function(i,n){
					var thislist = 
						'	<tr>'+				
						'		<td>'+n.id+'</td>'+				
						'		<td>'+n.province+'</td>'+
						'		<td>'+n.city+'</td>'+
						'		<td>'+n.area+'</td>'+
						'		<td>'+n.areaLength+'</td>'+
						'		<td>'+n.sendFee+'</td>'+							
						'		<td>'+
						'           <a class="btn btn-primary btn-xs" href="javascript:;" data-toggle="modal" data-target="#update" onclick="thisUpdate('+n.id+')"> 修改</a>'+
						'			<a class="btn btn-primary btn-xs" href="javascript:;" onclick="deleteeach('+n.id+')"> 删除</a>'+
                        '           <a class="btn btn-primary btn-xs" href="javascript:;" data-toggle="modal" data-target="#details" onclick="thisDetails('+n.id+')"> 详情</a>'+			
						'		</td>'+
						'	</tr>';
					$('#thislist').append(thislist);																				
				});
				$('#thiscount').text(data.data.total);
				$("#pager").pager({
					pagenumber: pageNo, 
					pagecount:data.data.pages,
					totalcount:data.data.total,
					buttonClickCallback: PageClick
				}); 
				
			} else if (data.code == 'OVERTIME'){
				var thisUrl = window.location.href;
				if (thisUrl.indexOf('login.html') <= -1) {
					top.window.location.href="login.html";
				}

			} else {
				if (data.msg != '空数据') {
					alert(data.msg)
				}else{
					$('#thiscount').text(0);
				}
			}

		},
		error:function() {
			alert("error");
		}
	});
}

//回调函数  
PageClick = function(pageclickednumber) {  
    init(pageclickednumber); 
}



//删除
function deleteeach(id){
	if(confirm("确定要删除吗？")) {
		deletegq(id);
	}
}

function deletegq(id){
	$.ajax({
		url: urlcore + "/api/sendRates/deleteSetting?id="+id,
		type: "get",
		dataType: 'json',
		contentType: "application/json;charset=utf-8",
		success:function(data){
			if (data.success == true) {
				init(1);
			} else if (data.code == 'OVERTIME'){
				var thisUrl = window.location.href;
				if (thisUrl.indexOf('login.html') <= -1) {
					top.window.location.href="login.html";
				}
			} else {
				alert(data.msg);
			}
		},
		error:function() {
			/* Act on the event */
			alert("error");
		}
	});
}

//配送费详情
function thisDetails(id) {
	$.ajax({
		url: urlcore + "/api/sendRates/settingDetail?id="+id,
		type: "get",
		dataType: 'json',
		contentType: "application/json;charset=utf-8",
		success:function(data){
			if (data.success == true) {
				var da = data.data;
				$('#sendFee_details').text(da.sendFee);
				$('#deliverTime_details').text(da.deliverTime);
				$('#areaLength_details').text(da.areaLength);
				$('#province_details').text(da.province);
				$('#city_details').text(da.city);
				$('#county_details').text(da.area);
	
			} else if (data.code == 'OVERTIME'){
				var thisUrl = window.location.href;
				if (thisUrl.indexOf('login.html') <= -1) {
					top.window.location.href="login.html";
				}

			} else {
				alert(data.msg);
			}

		},
		error:function() {
			/* Act on the event */
			alert("error");
		}
	});
}

//修改
function thisUpdate(id) {
	
	$.ajax({
		url: urlcore + "/api/sendRates/settingDetail?id="+id,
		type: "get",
		dataType: 'json',
		contentType: "application/json;charset=utf-8",
		success:function(data){
			if (data.success == true) {
				var da = data.data;
				$('#thisId_update').val(id);
				$('#sendFee_update').val(da.sendFee);
				$('#deliverTime_update').val(da.deliverTime);
				$('#areaLength_update').val(da.areaLength);
				//
	        	$('#province_update').val(da.province);
	        	$('#city_update').val(da.city);
	        	$('#county_update').val(da.area);
			} else if (data.code == 'OVERTIME'){
				var thisUrl = window.location.href;
				if (thisUrl.indexOf('login.html') <= -1) {
					top.window.location.href="login.html";
				}

			} else {
				alert(data.msg);
			}

		},
		error:function() {
			/* Act on the event */
			alert("error");
		}
	});	
}

function toUpdate() {
	//获取时间
	var provinceCode = $('#province .opts .selected').attr("val");
    var provinceName = $('#province .opts .selected').html();
    var cityCode = $('#city .opts .selected').attr("val");
    var cityName = $('#city .opts .selected').html();
    var areaCode = $('#area .opts .selected').attr("val");
    var areaName = $('#area .opts .selected').html();

    if (provinceName.indexOf("市") > -1 || provinceName.indexOf("香港") > -1 || provinceName.indexOf("澳门") > -1) {
        cityName = provinceName;
    }
    if (areaCode=='' || areaCode==undefined || areaCode=='undefined') {
            alert('请选择完整地区');
            return;
	}
	var id = $('#thisId_update').val();
	var sendFee = $('#sendFee_update').val();
	var deliverTime = $('#deliverTime_update').val();
	var areaLength = $('#areaLength_update').val();
	$.ajax({
        url:  urlcore + "/api/sendRates/addOrUpdateSendRates",
       		type: "post",
       		data:JSON.stringify([{
       			"id":id,
	            "areaCode": areaCode,
	            "areaLength": areaLength,
	            "deliverTime": deliverTime,
	            "sendFee": sendFee,
	            "province": provinceName,
	            "city": cityName,
	            "area": areaName
       		}]),
			dataType: 'json',
			contentType: "application/json;charset=utf-8",
			success:function(data){
				if (data.success == true) {             
                    if (confirm("修改成功!")) {
                        window.location.href = "setting_list.html";
                    } 	                   
                }
             else if (data.code == 'OVERTIME') {
                var thisUrl = window.location.href;
                if (thisUrl.indexOf('login.html') <= -1) {
                    top.window.location.href = "login.html";
                }
            } else {
                alert(data.msg);
            }

        },
        error: function() {
            alert("error");
        }

    });
}

//获得地区
function area(){
	var da='';
	$.ajax({
            url: urlcore + "/api/openArea/allArea",
            type: "get",
            dataType: 'json',
            contentType: "application/json;charset=utf-8",
            async: false,
            success:function(data){
                if (data.success == true) {
                    da = data.data;                   
	                /* $('#messUrl').html(imgStr2);                  
                    $('#title').text(da.title);
                    $('#content').html(da.content);*/
                } else if (data.code == 'OVERTIME'){
                    var thisUrl = window.location.href;
                    if (thisUrl.indexOf('login.html') <= -1) {
                        top.window.location.href="login.html";
                    }
                } else {
                    alert(data.msg);
                }
            },
            error:function() {
                /* Act on the event */
                alert("error");
            }
        });
        return da;
}

function changearea(){
	$("#change").change(function () {
		areaCode=$("#change").find("option:selected").val();
       	init(1);;
   	});	
	
}
